package com.zqh.volatileDemo;

import java.util.concurrent.TimeUnit;

/**
 * @author 曾庆红
 * {@code @date} 2023-10-11 21:08
 * volatile 会强制线程去堆内存中读取running的值
 */

public class T1 {
    volatile boolean running = true;
    void m(){
        System.out.println("m start");
        while(running){}
        System.out.println("m end");
    }
    public static void main(String[] args) {
        T1 t =new T1();
        new Thread(t::m,"T1").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t.running = false;
    }
}
